using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;


namespace OWED.Document
{
    public class DeleteTag: ChangeAtom
    {
        XmlNode my_tagtodelete;
        public DeleteTag(AbsolutePath deletePath, XmlNode todelete)
        {
            path = deletePath;
            my_tagtodelete = todelete;
        }

        public override void Apply(XmlDocument document)
        {
            XmlNode node = path.getNode(document);
            XmlNode parent = node.ParentNode;
            parent.RemoveChild(node);
        }

        public override void Reverse(XmlDocument document)
        {
            AbsolutePath p = new AbsolutePath(path, 1);
            (new AddTag(p, my_tagtodelete, 0)).Apply(document);
        }

        public XmlNode Node { get { return my_tagtodelete; } }

      

        public override string Serialize()
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateElement("root"));
            doc.FirstChild.AppendChild(doc.CreateElement("path"));
            doc.FirstChild.FirstChild.AppendChild(doc.CreateTextNode(path.ToString()));
            doc.FirstChild.AppendChild(doc.CreateElement("node"));
            doc.FirstChild.ChildNodes[1].AppendChild(doc.ImportNode(my_tagtodelete, false));

            return ChangeAtom.AtomType(this) +doc.OuterXml;
        }

        public static new ChangeAtom DeSerialize(String atom)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(atom);
            XmlNode node = doc.SelectSingleNode("/root/node").FirstChild;
            AbsolutePath path = new AbsolutePath(doc.SelectSingleNode("/root/path").FirstChild.Value);

            return new DeleteTag(path, node);
        }
    }
}
